<?php
/**
 * Created by PhpStorm.
 * User: wanghaibo
 * Date: 18/3/26
 * Time: 16:22
 */

namespace App\Http\Api\Admin;


use App\Http\API\ApiController;
use App\Services\Admin\UserService;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class UserApiController extends ApiController
{
    protected $service;
    public function __construct(UserService $service)
    {
        $this->service = $service;
    }

    /**
     * @param Request $request
     * @return array
     */
    protected function validator(Request $request){
        return $this->validate(
            $request,
            [
                'password' => 'required|min:6|confirmed',
                'email'     => 'required|unique:users',
                'token' => 'required',
            ]
        );
    }
    public function lists(Request $request){
        return $this->success(1,$this->service->lists($request));
    }
    public function store(Request $request){
        $validator = Validator::make(
            $request->all(),
            [
                'mobile'    =>  [
                    function($attribute,$value,$fail){
                        $IS_MOBILE = preg_match('/^0?(13|14|15|17|18)[0-9]{9}$/', $value, $matches);
                        if(!$IS_MOBILE){
                            return $fail('手机格式不正确');
                        }
                    }
                ],
                'name'    =>  'required|max:20',
                'email' =>  'required|email',
                'remark'  =>  'max:50',
            ]
        );
        if($validator->fails()){
            $msg = $validator->errors()->getMessages();
            $message = implode(' ',array_flatten($msg));
            return $this->fail(2,[],$message);
        }
        $this->service->add($request);
        return $this->success(1,[],'添加成功');

    }
    public function patch(Request $request){
        $act = $request->input('act','charge');
        return $this->{$act}($request);
    }
    public function destroy($user_id){
        if(!$user_id){
            return $this->fail(2,[],'参数错误');
        }
        if($user_id==1){
            return $this->fail(2,[],'该用户无法删除');
        }
        $user = $this->service->getUser($user_id);
        if(!$user){
            return $this->fail(2,[],'用户不存在');
        }
        $res = $this->service->destroy($user_id);
        if(!$res)$this->fail(2,[],'网络繁忙');
        return $this->success(1,[],'删除成功'.$user_id);
    }

    private function charge(Request $request){
        $validator = Validator::make(
            $request->all(),
            [
                'id'    =>  'required|exists:users,id',
                'amount'    =>  'required|numeric',
                'note'  =>  'max:255'
            ]
        );
        if($validator->fails()){
            $msg = $validator->errors()->getMessages();
            $message = implode(' ',array_flatten($msg));
            return $this->fail(2,[],$message);
        }
        $user_id = $request->input('id',0);
        if(!$user_id){
            return $this->fail(2,[],'参数错误');
        }
        $user = $this->service->getUser($user_id);
        if(!$user){
            return $this->fail(2,[],'用户不存在');
        }
        $amount = abs($request->input('amount',0));
        if($amount<=0){
            return $this->fail(2,[],'金额错误');
        }
        $this->service->charge($user_id,$amount,$request->input('note','用户充值'));

        return $this->success(1,[],'充值成功');
    }
    private function cost(Request $request){
        $validator = Validator::make(
            $request->all(),
            [
                'id'    =>  'required|exists:users,id',
                'amount'    =>  'required|numeric',
                'note'  =>  'max:255'
            ]
        );
        if($validator->fails()){
            $msg = $validator->errors()->getMessages();
            $message = implode(' ',array_flatten($msg));
            return $this->fail(2,[],$message);
        }
        $user_id = $request->input('id',0);
        if(!$user_id){
            return $this->fail(2,[],'参数错误');
        }
        $user = $this->service->getUser($user_id);
        if(!$user){
            return $this->fail(2,[],'用户不存在');
        }
        $amount = -1*abs($request->input('amount',0));
        if($amount>=0){
            return $this->fail(2,[],'金额错误');
        }
        $this->service->cost($user_id,$amount,$request->input('note','用户充值'));

        return $this->success(1,[],'消费成功');
    }
    private function update(Request $request){
        $validator = Validator::make(
            $request->all(),
            [
                'id'    =>  'required|exists:users,id',
                'name'    =>  'required|max:20',
                'remark'  =>  'max:50',
            ]
        );
        if($validator->fails()){
            $msg = $validator->errors()->getMessages();
            $message = implode(' ',array_flatten($msg));
            return $this->fail(2,[],$message);
        }
        $user_id = $request->input('id',0);
        if(!$user_id){
            return $this->fail(2,[],'参数错误');
        }
        $user = $this->service->getUser($user_id);
        if(!$user){
            return $this->fail(2,[],'用户不存在');
        }
        $res = $this->service->update($request,$user_id);
        if(!$res) return $this->fail(2,[],'网络繁忙');
        return $this->success(1,[],'编辑成功');
    }
}